package xyz.xtt.asset.entity;

import static xyz.xtt.common.asset.AssertUtils.isNull;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

import com.baomidou.mybatisplus.annotation.TableName;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.api.enums.ReferProtocolEnum;
import xyz.xtt.asset.constants.ErrorMessage;
import xyz.xtt.asset.enums.AccountStateEnum;

/**
 * <p>
 * 1155资产关系表
 * </p>
 *
 * @author author
 * @since 2024-12-25
 */
@Slf4j
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tbl_asset_account")
@ApiModel(value = "TblAssetTransactionRelation对象", description = "1155资产关系表")
public class TblAssetAccount implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "主键")
	private Long id;

	@ApiModelProperty(value = "帐户编号")
	private Long accountNo;

	@ApiModelProperty(value = "关联资产主键")
	private Long assetId;

	@ApiModelProperty(value = "拥有人id")
	private Long ownerId;

	@ApiModelProperty(value = "拥有数量")
	private BigDecimal amount;

	@ApiModelProperty(value = "创建时间")
	private LocalDateTime createTime;

	@ApiModelProperty(value = "创建时间")
	private LocalDateTime updateTime;

	@ApiModelProperty(value = "帐户状态")
	private Integer state;

	@ApiModelProperty(value = "版本")
	private Long ver;

	@ApiModelProperty(value = "协议版本")
	private Long referProtocol;
	
	@ApiModelProperty(value = "指导价")
	private BigDecimal guidePrice;

	@ApiModelProperty(value = "资产价格")
	private BigDecimal price;

	@ApiModelProperty(value = "出售方式0:出售1：寄售2：赠送3：拍卖")
	private Integer sellPattern;

	@ApiModelProperty(value = "是否可见")
	private Integer visiable;

	@ApiModelProperty(value = "上架时间")
	private Long lastPutOnSaleDatetime;

	@ApiModelProperty(value = "出售数量")
	private BigDecimal sellAmount;

	public Boolean isLock() {
		return Objects.equals(state, AccountStateEnum.LOCK.code());
	}

	public TblAssetAccount buildOther(Long accountNo, Long newTransactionId, Long to, BigDecimal amount) {
		return TblAssetAccount.builder().id(IdUtil.getSnowflakeNextId()).assetId(assetId).ownerId(to).amount(amount).createTime(LocalDateTime.now()).build();
	}

	public ReferProtocolEnum referProtocol() {
		ReferProtocolEnum convert = ReferProtocolEnum.convert(referProtocol);
		isNull(convert, ErrorMessage.UPSUPPORTED_PROTOCOL_TYPE, () -> log.info("from account is null"));
		return convert;
	}

}
